﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class shang_pin : Form
    {
        public shang_pin()
        {
            InitializeComponent(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {//修改商品
            try
            {
                Form22 b = new Form22(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[1].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[3].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[4].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[5].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[6].Value.ToString());
                b.ShowDialog();
                Table();//修改刷新
            }
            catch 
            {
                MessageBox.Show("错误");
            }
        }

        private void shang_pin_Load(object sender, EventArgs e)
        {//视图 表
            Table();
        }
        private void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Merchandise";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d, e, f, g;
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e = dr["Mpurchas"].ToString();
                f = dr["Mprince"].ToString();
                g = dr["Mstate"].ToString();

                string[] str = { a, b, c, d, e, f, g };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();
         
        }

        private void button3_Click(object sender, EventArgs e)
        {//删除商品
            try
            {
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号
                DialogResult dr = MessageBox.Show("确认删除"+id+":"+dataGridView1.SelectedRows[0].Cells[1].Value.ToString(),"提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    string sql1 = $"delete from Merchandise where Mid='{id}'";
                    Dao dao = new Dao();
                    if (dao.Execute(sql1) > 0)
                    {
                        MessageBox.Show("删除成功");
                        Table();//删除刷新
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                    
                }
            }
            catch
            {
                MessageBox.Show("该商品有相关订单，不可删除");
            }
        }

        

        private void dataGridView1_Click(object sender, EventArgs e)
        {//显示 当前选中
            label2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)//添加商品
        {
            shang_pin1 a = new shang_pin1();
            
            a.ShowDialog();
            Table();//添加刷新
        }

        private void button4_Click(object sender, EventArgs e)
        {//商品编号 查询 确认按钮
            id_search();
        }
        private void id_search()
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from Merchandise where Mid='{textBox1.Text}'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d, e, f, g;
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e = dr["Mpurchas"].ToString();
                f = dr["Mprince"].ToString();
                g = dr["Mstate"].ToString();

                string[] str = { a, b, c, d, e, f, g };
                dataGridView1.Rows.Add(str);

            }
            dr.Close();

        }
        private void name_search()
        {//查名 显示
            dataGridView1.Rows.Clear();
            string sql = $"select * from Merchandise where Mname like'%{textBox2.Text}%'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d, e, f, g;
                a = dr["Mid"].ToString();
                b = dr["Mname"].ToString();
                c = dr["Munit"].ToString();
                d = dr["Mnorm"].ToString();
                e = dr["Mpurchas"].ToString();
                f = dr["Mprince"].ToString();
                g = dr["Mstate"].ToString();

                string[] str = { a, b, c, d, e, f, g };
                dataGridView1.Rows.Add(str);

            }
            dr.Close();

        }

        private void button5_Click(object sender, EventArgs e)
        {//商品名称 查询 确认按钮
            name_search();
            
        }

        private void button6_Click(object sender, EventArgs e)
        {//显示所有商品
            Table();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.SelectedRows.Count;//获取当前选中的行数
            DialogResult dr = MessageBox.Show($"确认删除 {n}条商品信息", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string sql = $"delete from Merchandise where Mid in (";
                for (int i = 0; i < n; i++)
                {
                    sql += $"'{dataGridView1.SelectedRows[i].Cells[0].Value.ToString()}',";
                }
                sql = sql.Remove(sql.Length - 1);//删除最后一个字符
                sql += ")";
                Dao dao = new Dao();
                if (dao.Execute(sql) > n - 1)
                {
                    MessageBox.Show($"成功删除 {n}条商品信息");
                    Table();
                }

            }      
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
